from enum import Enum, auto
from PyQt5.QtWidgets import (QWidget, QPushButton, QLabel, QLineEdit, QFrame, 
                            QHBoxLayout, QVBoxLayout, QScrollArea, QSplitter,
                            QToolBar, QMenuBar, QMenu, QAction, QActionGroup,
                            QFileDialog, QMessageBox, QDialog, QTextEdit,
                            QProgressBar)
from PyQt5.QtCore import Qt, pyqtSignal, QSize
from PyQt5.QtGui import QIcon, QKeySequence

class ControlId(Enum):
    """控件ID枚举：统一管理所有控件的标识符"""
    # 主窗口控件
    MAIN_WINDOW = auto()
    CENTRAL_WIDGET = auto()
    SPLITTER = auto()
    VIEWER_CONTAINER = auto()
    THUMBNAIL_SIDEBAR = auto()
    
    # 菜单栏和工具栏
    MENU_BAR = auto()
    TOOL_BAR = auto()
    
    # 文件菜单动作
    ACTION_OPEN = auto()
    ACTION_SAVE = auto()
    ACTION_SAVE_AS = auto()
    ACTION_EXPORT_WORD = auto()
    ACTION_EXIT = auto()
    
    # 视图菜单动作
    ACTION_SHOW_THUMB = auto()
    ACTION_ZOOM_IN = auto()
    ACTION_ZOOM_OUT = auto()
    ACTION_RESET_ZOOM = auto()
    ACTION_SAVE_MODE_AS = auto()
    ACTION_SAVE_MODE_DIRECT = auto()
    
    # 文本菜单动作
    ACTION_EXTRACT_TEXT = auto()
    
    # 页面导航控件
    BTN_PREV_PAGE = auto()
    BTN_NEXT_PAGE = auto()
    EDIT_PAGE = auto()
    LBL_PAGE_COUNT = auto()  # 添加页码计数标签ID
    
    # 缩放控件
    BTN_ZOOM_IN = auto()
    BTN_ZOOM_OUT = auto()
    BTN_RESET_ZOOM = auto()
    LBL_ZOOM = auto()
    
    # 页面显示控件
    SCROLL_AREA = auto()
    PAGES_CONTAINER = auto()
    PAGES_LAYOUT = auto()
    OVERLAY_CONTAINER = auto()
    SELECTION_LAYER = auto()
    
    # 导出对话框
    EXPORT_DIALOG = auto()
    EXPORT_TEXT_EDIT = auto()
    BTN_COPY_TEXT = auto()
    BTN_CANCEL_EXPORT = auto()

class ControlManager:
    """控件管理器：创建和管理所有UI控件"""
    
    def __init__(self, parent):
        self.parent = parent
        self._controls = {}
        self._create_controls()
    
    def _create_controls(self):
        """创建所有UI控件"""
        # 创建主容器和分割器
        self._create_main_containers()
        
        # 创建菜单栏和工具栏
        self._create_menus_and_toolbars()
        
        # 创建页面导航控件
        self._create_navigation_controls()
        
        # 创建页面显示控件
        self._create_page_viewer()
        
        # 创建导出对话框
        self._create_export_dialog()
    
    def _create_main_containers(self):
        """创建主容器控件"""
        central_widget = QWidget()
        self._controls[ControlId.CENTRAL_WIDGET] = central_widget
        
        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        
        # 创建分割器
        splitter = QSplitter(Qt.Horizontal)
        self._controls[ControlId.SPLITTER] = splitter
        main_layout.addWidget(splitter)
        
        # 创建主视图容器
        viewer_container = QWidget()
        viewer_layout = QVBoxLayout(viewer_container)
        viewer_layout.setContentsMargins(5, 5, 5, 5)
        self._controls[ControlId.VIEWER_CONTAINER] = viewer_container
        splitter.addWidget(viewer_container)
    
    def _create_navigation_controls(self):
        """创建页面导航和缩放控件"""
        viewer_container = self.get(ControlId.VIEWER_CONTAINER)
        
        # 创建导航工具栏
        nav_toolbar = QToolBar("导航")
        nav_toolbar.setIconSize(QSize(16, 16))
        viewer_container.layout().addWidget(nav_toolbar)
        
        # 上一页按钮
        prev_btn = QPushButton("上一页")
        self._controls[ControlId.BTN_PREV_PAGE] = prev_btn
        nav_toolbar.addWidget(prev_btn)
        
        # 页码输入
        page_edit = QLineEdit()
        page_edit.setMaximumWidth(50)
        page_edit.setText("1")
        self._controls[ControlId.EDIT_PAGE] = page_edit
        nav_toolbar.addWidget(page_edit)
        
        # 页码计数标签（新增）
        page_count_label = QLabel("/ 0")
        self._controls[ControlId.LBL_PAGE_COUNT] = page_count_label
        nav_toolbar.addWidget(page_count_label)
        
        # 下一页按钮
        next_btn = QPushButton("下一页")
        self._controls[ControlId.BTN_NEXT_PAGE] = next_btn
        nav_toolbar.addWidget(next_btn)
        
        nav_toolbar.addSeparator()
        
        # 缩放控件
        zoom_in_btn = QPushButton("放大")
        self._controls[ControlId.BTN_ZOOM_IN] = zoom_in_btn
        nav_toolbar.addWidget(zoom_in_btn)
        
        zoom_out_btn = QPushButton("缩小")
        self._controls[ControlId.BTN_ZOOM_OUT] = zoom_out_btn
        nav_toolbar.addWidget(zoom_out_btn)
        
        reset_zoom_btn = QPushButton("重置")
        self._controls[ControlId.BTN_RESET_ZOOM] = reset_zoom_btn
        nav_toolbar.addWidget(reset_zoom_btn)
        
        zoom_label = QLabel("100%")
        self._controls[ControlId.LBL_ZOOM] = zoom_label
        nav_toolbar.addWidget(zoom_label)
    
    def _create_menus_and_toolbars(self):
        """创建菜单栏和工具栏"""
        # 创建菜单栏
        menu_bar = QMenuBar()
        self._controls[ControlId.MENU_BAR] = menu_bar
        
        # 创建文件菜单
        file_menu = menu_bar.addMenu("文件")
        
        open_action = QAction("打开", self.parent)
        self._controls[ControlId.ACTION_OPEN] = open_action
        file_menu.addAction(open_action)
        
        save_action = QAction("保存", self.parent)
        self._controls[ControlId.ACTION_SAVE] = save_action
        file_menu.addAction(save_action)
        
        save_as_action = QAction("另存为", self.parent)
        self._controls[ControlId.ACTION_SAVE_AS] = save_as_action
        file_menu.addAction(save_as_action)
        
        file_menu.addSeparator()
        
        export_action = QAction("导出为Word", self.parent)
        self._controls[ControlId.ACTION_EXPORT_WORD] = export_action
        file_menu.addAction(export_action)
        
        file_menu.addSeparator()
        
        exit_action = QAction("退出", self.parent)
        self._controls[ControlId.ACTION_EXIT] = exit_action
        file_menu.addAction(exit_action)
        
        # 创建视图菜单
        view_menu = menu_bar.addMenu("视图")
        
        show_thumb_action = QAction("显示缩略栏", self.parent, checkable=True)
        show_thumb_action.setChecked(True)
        self._controls[ControlId.ACTION_SHOW_THUMB] = show_thumb_action
        view_menu.addAction(show_thumb_action)
        
        view_menu.addSeparator()
        
        zoom_in_action = QAction("放大", self.parent)
        self._controls[ControlId.ACTION_ZOOM_IN] = zoom_in_action
        view_menu.addAction(zoom_in_action)
        
        zoom_out_action = QAction("缩小", self.parent)
        self._controls[ControlId.ACTION_ZOOM_OUT] = zoom_out_action
        view_menu.addAction(zoom_out_action)
        
        reset_zoom_action = QAction("重置缩放", self.parent)
        self._controls[ControlId.ACTION_RESET_ZOOM] = reset_zoom_action
        view_menu.addAction(reset_zoom_action)
        
        view_menu.addSeparator()
        
        # 保存模式子菜单
        save_mode_menu = view_menu.addMenu("保存模式")
        save_mode_as = QAction("另存为模式", self.parent, checkable=True)
        save_mode_as.setChecked(True)
        self._controls[ControlId.ACTION_SAVE_MODE_AS] = save_mode_as
        
        save_mode_direct = QAction("直接保存模式", self.parent, checkable=True)
        self._controls[ControlId.ACTION_SAVE_MODE_DIRECT] = save_mode_direct
        
        save_mode_menu.addAction(save_mode_as)
        save_mode_menu.addAction(save_mode_direct)
        
        # 创建文本菜单
        text_menu = menu_bar.addMenu("文本")
        
        extract_text_action = QAction("提取选中文本", self.parent)
        self._controls[ControlId.ACTION_EXTRACT_TEXT] = extract_text_action
        text_menu.addAction(extract_text_action)
        
        # 创建工具栏
        tool_bar = QToolBar("工具栏")
        tool_bar.setIconSize(QSize(16, 16))
        self._controls[ControlId.TOOL_BAR] = tool_bar
        
        # 向工具栏添加常用动作
        tool_bar.addAction(open_action)
        tool_bar.addAction(save_action)
        tool_bar.addSeparator()
        tool_bar.addAction(zoom_in_action)
        tool_bar.addAction(zoom_out_action)
        tool_bar.addAction(reset_zoom_action)
        tool_bar.addSeparator()
        tool_bar.addAction(extract_text_action)
    
    def _create_page_viewer(self):
        """创建页面显示区域控件"""
        viewer_container = self.get(ControlId.VIEWER_CONTAINER)
        
        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidgetResizable(True)
        scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self._controls[ControlId.SCROLL_AREA] = scroll_area
        viewer_container.layout().addWidget(scroll_area)
        
        # 页面容器
        pages_container = QWidget()
        pages_layout = QVBoxLayout(pages_container)
        pages_layout.setAlignment(Qt.AlignCenter)
        pages_layout.setSpacing(20)
        self._controls[ControlId.PAGES_CONTAINER] = pages_container
        self._controls[ControlId.PAGES_LAYOUT] = pages_layout
        scroll_area.setWidget(pages_container)
        
        # 创建覆盖层容器（用于文本选择）
        overlay_container = QWidget(pages_container)
        overlay_container.setGeometry(0, 0, 0, 0)
        overlay_container.setStyleSheet("background: transparent;")
        overlay_container.setAttribute(Qt.WA_TransparentForMouseEvents, False)
        self._controls[ControlId.OVERLAY_CONTAINER] = overlay_container
        
        # 选择层（用于显示选择框）
        selection_layer = QFrame(overlay_container)
        selection_layer.setGeometry(0, 0, 0, 0)
        selection_layer.setStyleSheet("background: rgba(0, 120, 215, 0.2); border: 1px solid #0078d7;")
        selection_layer.hide()
        self._controls[ControlId.SELECTION_LAYER] = selection_layer
    
    def _create_export_dialog(self):
        """创建文本导出对话框"""
        dialog = QDialog(self.parent)
        dialog.setWindowTitle("提取的文本")
        dialog.resize(600, 400)
        self._controls[ControlId.EXPORT_DIALOG] = dialog
        
        layout = QVBoxLayout(dialog)
        
        # 文本编辑区域
        text_edit = QTextEdit()
        text_edit.setReadOnly(True)
        self._controls[ControlId.EXPORT_TEXT_EDIT] = text_edit
        layout.addWidget(text_edit)
        
        # 按钮区域
        btn_layout = QHBoxLayout()
        layout.addLayout(btn_layout)
        
        copy_btn = QPushButton("复制文本")
        self._controls[ControlId.BTN_COPY_TEXT] = copy_btn
        btn_layout.addWidget(copy_btn)
        
        btn_layout.addStretch()
        
        cancel_btn = QPushButton("取消")
        self._controls[ControlId.BTN_CANCEL_EXPORT] = cancel_btn
        btn_layout.addWidget(cancel_btn)
    
    def get(self, control_id):
        """通过ID获取控件"""
        return self._controls.get(control_id)
